package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.consumerFragment;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.json.JSONObject;

import java.util.Random;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity.ActivityVerification;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Trace;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.SmsBroadcastReceiver;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.ProductServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.ImageUtils;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.JSONObjectUtils;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.QRCodeUtils;

public class FragmentSearchTrace extends Fragment {

    private Button addQRCode;
    private ImageView iQRCode;

    private ProductServiceConnection productConnection;
    private SmsBroadcastReceiver smsBroadcastReceiver;

    private static final int QR_CODE_REQUEST = 1;  // 定义请求码

    public FragmentSearchTrace(ProductServiceConnection productConnection) {
        this.productConnection = productConnection;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (smsBroadcastReceiver != null) {
            requireContext().unregisterReceiver(smsBroadcastReceiver);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_trace, container, false);

        addQRCode = view.findViewById(R.id.btn_add_product);
        iQRCode = view.findViewById(R.id.product_QR);

        addQRCode.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(intent, QR_CODE_REQUEST); // 请求二维码
        });

        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QR_CODE_REQUEST) {
            if (resultCode == getActivity().RESULT_OK && data != null) {
                Uri selectedImageUri = data.getData();
                iQRCode.setImageURI(selectedImageUri);

                QRCodeUtils.readQRCodeFromBitmap(ImageUtils.drawable2bitmap(iQRCode.getDrawable()), data1 -> {
                    if (data1 != null) {

                        // 跳转到新的Activity，显示验证码输入界面
                        Intent intent = new Intent(getActivity(), ActivityVerification.class);
                        startActivity(intent);

                    } else {
                        Toast.makeText(getActivity(), "未能识别二维码重新选择", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

}

